<?php


namespace App\Http\Controllers\Admin\Sale;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Invite;
use App\Models\Sales;
use Illuminate\Support\Facades\DB;

class SalesController extends BaseAdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');
        $query = Sales::orderBy($sort, $order);
        if (request('title')) {
            $query->where('title', 'LIKE', "%" . request('title') . "%");
        }
        if(request('nickname')){
            $query->where('nickname','LIKE', "%" . request('nickname') . "%");
        }
        if(request('invitor1_nickname')){
            $query->where('invitor1_nickname','LIKE', "%" . request('invitor1_nickname') . "%");
        }
        if(request('invitor2_nickname')){
            $query->where('invitor2_nickname','LIKE', "%" . request('invitor2_nickname') . "%");
        }
        // 取得列表
        $signs = $query->select([
            'id',
            'uid',
            'nickname',
            'nickname',
            'status',
            'created_at',
            'money',
            'title',
            'invitor1',
            'invitor2',
            'invitor1_nickname',
            'invitor2_nickname'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $signs->appends(app('request')->all());

        return view('admin.sale.sales', [
            'sales' => $signs,
        ]);
    }
    /**
     * 商家关闭和显示
     *  @api changeShop
     * @apiParams id
     * @return \Illuminate\Http\JsonResponse
     * @author yh 20200406
     *
     **/
    public function changeSale($id){
        $shop = Sales::where('id',$id)->first();
        $status = $shop->status;
        if($status==2){
            $shop->status=1;
           
        }else{
            $shop->status=2;
        }
        $shop->save();
        return $this->success();
    }
}